package com.ruoyi.system.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Dailyschedule;
import com.ruoyi.system.domain.DailyscheduleExcelData;
import com.ruoyi.system.service.IDailyscheduleService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 作息时间解析Controller
 *
 * @author ruoyi
 * @date 2024-03-24
 */
@RestController
@RequestMapping("/system/dailyschedule")
public class DailyscheduleController extends BaseController {
    @Autowired
    private IDailyscheduleService dailyscheduleService;

    /**
     * 上传课程表并解析
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dailyschedule:upload')")
    @ApiOperation("上传时间表并解析")
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file, int isCover) throws IOException {
        int uploadAndSaveCourse = dailyscheduleService.uploadAndSaveCourse(file, isCover);
        if (uploadAndSaveCourse == 2) {
            return AjaxResult.error("未从表格中读取年度或季度格式错误");
        }
        if (uploadAndSaveCourse == 3) {
            return AjaxResult.error("当前表格中有系统中已存在的时间信息,是否需要覆盖");
        }
        if (uploadAndSaveCourse == 4) {
            return AjaxResult.error("上传文件失败,请检查格式,文件需为.xlsx类型");
        }
        if (uploadAndSaveCourse == 5) {
            return AjaxResult.error("表格中年度或季度都不能为空,请检查");
        }
        return toAjax(1);
    }

    /**
     * 查询作息时间解析列表
     */
    @PreAuthorize("@ss.hasPermi('system:dailyschedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dailyschedule dailyschedule) {
        startPage();
        List<Dailyschedule> list = dailyscheduleService.selectDailyscheduleList(dailyschedule);
        return getDataTable(list);
    }

    /**
     * 导出作息时间解析列表
     */
    @PreAuthorize("@ss.hasPermi('system:dailyschedule:export')")
    @Log(title = "作息时间解析", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, Dailyschedule dailyschedule) {
        List<Dailyschedule> list = dailyscheduleService.selectDailyscheduleList(dailyschedule);
        List<DailyscheduleExcelData> data = Lists.newArrayList();
        StringBuilder subTile = new StringBuilder();
        if (!CollectionUtils.isEmpty(list)) {
            Dailyschedule d = list.get(0);
            subTile.append(d.getYearTime()).append("学年").append(d.getSeason()).append("季").append("作息时间表");
            List<String> periodList = JSON.parseObject(d.getTimePeriod(), new TypeReference<List<String>>() {
            });
            List<String> startList = JSON.parseObject(d.getStartTime(), new TypeReference<List<String>>() {
            });
            List<String> endList = JSON.parseObject(d.getEndTime(), new TypeReference<List<String>>() {
            });
            List<String> contentList = JSON.parseObject(d.getContent(), new TypeReference<List<String>>() {
            });
            for (int i = 0; i < periodList.size(); i++) {
                data.add(new DailyscheduleExcelData(periodList.get(i), startList.get(i), endList.get(i), contentList.get(i)));
            }
        }
        ExcelUtil<DailyscheduleExcelData> util = new ExcelUtil<>(DailyscheduleExcelData.class);
        util.exportExcel(response, data, "作息时间解析数据", subTile.toString());
    }

    /**
     * 获取作息时间解析详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dailyschedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dailyscheduleService.selectDailyscheduleById(id));
    }

    /**
     * 新增作息时间解析
     */
    @PreAuthorize("@ss.hasPermi('system:dailyschedule:add')")
    @Log(title = "作息时间解析", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dailyschedule dailyschedule) {
        return toAjax(dailyscheduleService.insertDailyschedule(dailyschedule));
    }

    /**
     * 修改作息时间解析
     */
    @PreAuthorize("@ss.hasPermi('system:dailyschedule:edit')")
    @Log(title = "作息时间解析", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dailyschedule dailyschedule) {
        return toAjax(dailyscheduleService.updateDailyschedule(dailyschedule));
    }

    /**
     * 删除作息时间解析
     */
    @PreAuthorize("@ss.hasPermi('system:dailyschedule:remove')")
    @Log(title = "作息时间解析", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dailyscheduleService.deleteDailyscheduleByIds(ids));
    }


}
